package com.xiaoy.core.email.service;

import org.springframework.stereotype.Service;

@Service("emailTimeService")
public class EmailTimeService extends EmailSerivce{

//	@Autowired
//	private UserDao userDao;
	
//	@Resource
//	private BpmDao bpmDao;
	
	public void sendTimeEmail (){
		
		// 获得所有的代办
		/*String sqlUser = "select t.assignees "
				+"from wftask t "
				+"where t.state = 'ASSIGNED' "
				+"order by t.instanceid desc ";
		
		// 获得有待办的人  ----  start  ----
		Set<String> userSet = new HashSet<String>();
		
		List<Map<String,Object>>assignees = bpmDao.query(sqlUser);
	
		for (Map<String,Object>assigne: assignees) {
			String array = StringUtil.ObjectToString(assigne.get("ASSIGNEES"));
			if (!"".equals(array)) {
				if (array.indexOf(",")!=-1) {
					String[] us = array.split(",");
					for (String u:us) {
						userSet.add(u);
					}
				}else{
					userSet.add(array);
				}
			}
		}
		// 获得有待办的人  ----  end  ----
		
		
		String sql = "select t.assignees,t.assigneesdisplayname,t.callbackcontext,t.creator,t.instanceid,t.processid,t.processname,"
				+"t.approvers,t.assigneddate,t.createddate,t.enddate,t.state,t.taskid,t.tasknumber,t.protectedtextattribute1"
				+",t.protectedtextattribute2,t.protectedtextattribute3,t.title,t.taskdefinitionname,t.activityname,t.activityid,t.thread" 
				+" from wftask t "
				+"where t.state = 'ASSIGNED' and instr(t.assigneesdisplayname,'${user}') > 0 "
				+"order by t.instanceid desc";
		
		// 获得人员对应的所有的待办  ---- start  
		Map<String,Object>tasks = new HashMap<String, Object>();
		for (String user:userSet) {
			String s = sql.replace("${user}", user);
			List<Map<String,Object>> list = bpmDao.query(s);
			if (list.size()>0) {
				tasks.put(user, list);
			}
		}
		// 获得人员对应的所有的待办  ---- end  
		
		
		// 发送邮件 --start
		List<Email> emails = new ArrayList<Email>();
		
//		获得邮箱
		StringBuilder sb = null;
		for (String user:userSet) {
			
			Object obj = tasks.get(user);
			if (obj==null) {
				break;
			}
			
			// 获得用户信息
			User u = userDao.getUserByLoginName(user);
			Email mail = new Email();
			sb = new StringBuilder("");
			List<Map<String,Object>>tasksList = (List<Map<String, Object>>) obj;
			for (Map<String,Object>taskinfo:tasksList) {
				String taskdefinitionname = StringUtils.ObjectToString(taskinfo.get("TASKDEFINITIONNAME"));
				String createUser = StringUtils.ObjectToString(taskinfo.get("CREATOR"));
				
				sb.append(tr.replace("${taskname}", "<a href='http：//www.baidu.com'>"+taskdefinitionname+"</a>"))
				  .append(tr.replace("${createUser}", createUser));
			}
			
			mail.setContent(table.replace("${tr}", sb.toString()));
			mail.setTo_mail(u.getEmail());
			mail.setTo_name(u.getEmpuid());
			mail.setTo_nick(u.getDisplayname());
			mail.setSubject("待办任务");
			emails.add(mail);
		}
		sends(emails);*/
		//  发送邮件  --- end
	}
	
	private String table = "<table><tr><th>任务名称</th><th>发起人</th><tr>${tr}<table>";
	private String tr = "<tr><td>${taskname}</td><td>${createUser}</td></tr>";
}
